package com.ohuang.apkpatch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

public class ExecUtil {

    /**
     * Execute system command
     *
     * @param cmd
     * @param timeOut time out
     * @return the result
     * @throws IOException
     * @throws InterruptedException
     * @author lyrics
     * @since 2020/06/15
     */
    public static String exec(String [] cmd ,int timeOut) throws IOException, InterruptedException {
        Process p = Runtime.getRuntime().exec(cmd);
        boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
        if(!res) {
            return "Time out";
        }
        InputStream inputStream = p.getInputStream();
        byte[] data = new byte[1024];
        StringBuilder result = new StringBuilder();
        while(inputStream.read(data) != -1) {
            result.append(new String(data, StandardCharsets.UTF_8));
        }
        if (result.toString().equals("")) {
            InputStream errorStream = p.getErrorStream();
            while(errorStream.read(data) != -1) {
                result.append(new String(data, StandardCharsets.UTF_8));
            }
        }
        return result.toString();

    }

    public static String start(String [] cmd ,int timeOut) throws IOException, InterruptedException {
        Process p = new ProcessBuilder(cmd).start();
        boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
        if(!res) {
            return "Time out";
        }
        InputStream inputStream = p.getInputStream();
        byte[] data = new byte[1024];
        StringBuilder result = new StringBuilder();
        while(inputStream.read(data) != -1) {
            result.append(new String(data, "GBK"));
        }
        if (result.toString().equals("")) {
            InputStream errorStream = p.getErrorStream();
            while(errorStream.read(data) != -1) {
                result.append(new String(data, "GBK"));
            }
        }
        return result.toString();

    }

    public static String execGbk(String [] cmd ,int timeOut) throws IOException, InterruptedException {

        Process p = Runtime.getRuntime().exec(cmd);
        boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
        if(!res) {
            return "Time out";
        }
        InputStream inputStream = p.getInputStream();
        byte[] data = new byte[1024];
        StringBuilder result = new StringBuilder();
        while(inputStream.read(data) != -1) {
            result.append(new String(data, "GBK"));
        }
        if (result.toString().equals("")) {
            InputStream errorStream = p.getErrorStream();
            while(errorStream.read(data) != -1) {
                result.append(new String(data, "GBK"));
            }
        }
        return result.toString();

    }

    public static String execAndPrint(String [] cmd ,int timeOut) throws IOException, InterruptedException {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
        String line = null;
        while ((line = reader.readLine()) != null){
            System.out.println(line);
        }

        boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
        if(!res) {
            return "Process Time out";
        }

        return "Process end";

    }
    public static String startAndPrint(String [] cmd ,int timeOut) throws IOException, InterruptedException {
        Process p = new ProcessBuilder(cmd).start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
        String line = null;
        while ((line = reader.readLine()) != null){
            System.out.println(line);
        }

        boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
        if(!res) {
            return "Process Time out";
        }

        return "Process end";

    }
    public static String execAndPrint(String [] cmd ,int timeOut,String readStringStop) throws IOException, InterruptedException {
        Process p = Runtime.getRuntime().exec(cmd);
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
        String line = null;
        while ((line = reader.readLine()) != null){
            System.out.println(line);
            if (line.startsWith(readStringStop)){
                p.destroy();
            }
        }

        boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
        if(!res) {
            return "Process Time out";
        }

        return "Process end";

    }
    public static String startAndPrint(String [] cmd ,int timeOut,String readStringStop) throws IOException, InterruptedException {
        Process p = new ProcessBuilder(cmd).start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
        String line = null;
        while ((line = reader.readLine()) != null){
            System.out.println(line);
            if (line.startsWith(readStringStop)){
                p.destroy();
            }
        }

        boolean res = p.waitFor(timeOut, TimeUnit.SECONDS);
        if(!res) {
            return "Process Time out";
        }

        return "Process end";

    }



}
